<?php

namespace app\common\dao\system;

use app\common\model\system\SystemRole as model;
use app\common\dao\BaseDao;

class SystemRoleDao extends BaseDao
{
    /**
    * 设置模型
    * @return model
    */
    protected function getModel(): string
    {
        return model::class;
    }


    public function search(string $type, array $where = [])
    {
        $roleModel = ($this->getModel())::getInstance();

        if (isset($where['role_name'])) {
            $roleModel = $roleModel->whereLike('role_name', '%' . $where['role_name'] . '%');
        }

        if (isset($where['status'])) {
            $roleModel = $roleModel->where('status', intval($where['status']));
        }
        return $roleModel->where('type', $type);
    }


    public function getAllOptions(string $type)
    {
        return ($this->getModel())::getDB()->where('status', 1)->where('type', $type)->column('role_name', 'role_id');
    }

    public function idsByRules(string $type, array $ids)
    {
        $rules = ($this->getModel())::getDB()->where('status', 1)->where('type', $type)->whereIn($this->getPk(), $ids)->column('rules');
        $data = [];
        foreach ($rules as $rule) {
            $data = array_merge(explode(',', $rule), $data);
        }
        return array_unique($data);
    }

    public function mExists(string $type, int $id)
    {
        return ($this->getModel())::getDB()->where($this->getPk(), $id)->where('type', $type)->count() > 0;
    }


}